package com.ms.game.jackpot;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.jackpot.message.CGetGameJackPotMoney;
import com.ms.game.jackpot.message.SGameBrocastJackpotMoneyResponse;
import com.ms.game.jackpot.message.SGameJackpotCacheResquest;
import com.ms.game.jackpot.message.SGameNotifyJackpotMoney;
import com.ms.game.jackpot.message.SGetGameJackpotMoneyResponse;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.processer.BasiceServiceProcesser;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.group.transportlayer.BroadcastTargetTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;

public class JackPotBrocastProcesser  extends BasiceServiceProcesser
{
	
	private static final Log log = LogFactory.getLog(JackPotBrocastProcesser.class);
	
	/**
	 *  GC Spend time
	 * 所有遊戲jp money
	 */
/*	public static HashMap<String , HashMap<String,ArrayList<JackPotMoneyData>>> 
							jackpot = new HashMap<String , HashMap<String,ArrayList<JackPotMoneyData>>>();*/
	
	public static Map<String , HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>>> jackpot = 
			Collections.synchronizedMap(new HashMap<String , HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>>>());
	
	
	/**
	 * 更新遊戲jp彩金
	 */
	public void revNotifyJackpotMoney(SGameNotifyJackpotMoney request)
	{
		String gameType = request.gameName;
		String hallType = request.halltype;
		
		if(!jackpot.containsKey(gameType))
		{
			if(log.isErrorEnabled())
			{
				log.error("No Cache This Game Jackpot Data : " +request.gameName);
			}
			return;
		}
		
		HashMap<Integer, ArrayList<JackPotMoneyData>> jpdata = request.jpdata;
		
		//更新
		HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>> data = jackpot.get(gameType);
		if (data == null || !data.containsKey(hallType)) {
			if (log.isErrorEnabled()) {
				log.error("[" + gameType + "] No Cache This HallType Jackpot Data : " + hallType);
			}
			return;
		}
		
		//暫存的資料
		HashMap<Integer, ArrayList<JackPotMoneyData>> cacheList = data.get(hallType);
		
		//對更新的資訊loop
		for(Entry<Integer, ArrayList<JackPotMoneyData>> entry : jpdata.entrySet()){
			int gainRateID = entry.getKey();
			if (!cacheList.containsKey(gainRateID)) {
				if (log.isErrorEnabled()) {
					log.error("[" + gameType + " / " + hallType + "] No Cache This gainRateID Jackpot Data : "
							+ gainRateID);
				}
				continue;
			}
			
			ArrayList<JackPotMoneyData> updateDatas = entry.getValue();
			ArrayList<JackPotMoneyData> cacheDatas = cacheList.get(gainRateID);
			for(JackPotMoneyData updateData : updateDatas){
				for(int cacheIDX = 0; cacheIDX < cacheDatas.size(); cacheIDX++){
					JackPotMoneyData cacheData = cacheDatas.get(cacheIDX);
					
					//如果是同一個iconKey的就更新
					if(GameUtility.safeStringEquals(updateData.iconkey, cacheData.iconkey)){
						cacheDatas.set(cacheIDX, updateData);
					}
				}
			}
		}
		
	}
	
	
	/**
	 * 讀取遊戲jp彩金
	 */
	public void revGetJackpotMoney(CGetGameJackPotMoney request)
	{
		GUID sessionid = this.baseaction.getSessionID();
		String gameName = request.getGameName();
		String halltype = request.getHalltype();
		
		SGetGameJackpotMoneyResponse response = new SGetGameJackpotMoneyResponse();
		
		if(!jackpot.containsKey(gameName) && !gameName.equals(JackPotBrocastModule.ALL_GAME))
		{
			if(log.isErrorEnabled())
			{
				log.error("No Cache This Game Jackpot Data : " +request.getGameName());
			}
			
			
			response.setErrorCode(ErrorCode.JACKPOT_NOT_FOUND);
			MessageAction.sendMessage( sessionid, MessageTypes.S_GET_JACKPOT_MONEY_BACK, response);
			return;
		}
		
		
		if(gameName.equals(JackPotBrocastModule.ALL_GAME))
		{
			response.setJpdata
			(
					new HashMap<String, HashMap<String,HashMap<Integer,ArrayList<JackPotMoneyData>>>>(jackpot)
			);
		}
		else
		{
			HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>> jpdata = jackpot.get(gameName);
			HashMap<String, HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>>> data = new HashMap<String, HashMap<String,HashMap<Integer,ArrayList<JackPotMoneyData>>>>();
			if(halltype.equals(JackPotBrocastModule.ALL_GAME))
			{
				data.put(gameName,jpdata);
			}
			else
			{
				if(!jpdata.containsKey(halltype))
				{
					if(log.isErrorEnabled())
					{
						log.error("No Cache This Game Jackpot Data : " +request.getGameName() + " halltype : " + ((halltype!=null)? halltype:"NULL"));
					}
					
					response.setErrorCode(ErrorCode.JACKPOT_NOT_FOUND);
					MessageAction.sendMessage( sessionid, MessageTypes.S_GET_JACKPOT_MONEY_BACK, response);
					return;
				}
				
				HashMap<String, HashMap<Integer, ArrayList<JackPotMoneyData>>> halldata = new HashMap<String, HashMap<Integer,ArrayList<JackPotMoneyData>>>();
				halldata.put(halltype, jpdata.get(halltype));
				data.put(gameName,halldata);
			}
			response.setJpdata(data);
		}
		
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage( sessionid, MessageTypes.S_GET_JACKPOT_MONEY_BACK, response);
	}
	
	/**
	 * 快取 jp data
	 * @param request
	 */
	public void revCacheJackpotMoney(SGameJackpotCacheResquest request)
	{
		String gamename = request.gameName;
		String srcMID = request.srcModuleid;
		String halltype = request.halltype;
		
		HashMap<String, HashMap<Integer,ArrayList<JackPotMoneyData>>> halldata = null;
		if(jackpot.containsKey(gamename))
		{
			halldata = jackpot.get(gamename);
		}
		else
		{
			halldata = new HashMap<String, HashMap<Integer,ArrayList<JackPotMoneyData>>>();
			jackpot.put(gamename, halldata);
		}
		
		halldata.put(halltype, request.jpdata);
		
		//通知快取成功
		GroupMessage msg = new GroupMessage();
		msg.setMessageType(MessageTypes.S_GAME_CACHE_JACKPOT_OK);
		msg.setMessageContent(null);
		msg.setDestModuleID(srcMID);
		MessageAction.sendGroupMessage(msg);
	}
	
	/**
	 * 廣播所有jp data
	 */
	public void onBrocastAllGameJackpot()
	{
		SGameBrocastJackpotMoneyResponse response = new SGameBrocastJackpotMoneyResponse();
		response.setJackpots
		(
				new HashMap<String, HashMap<String,HashMap<Integer,ArrayList<JackPotMoneyData>>>>(jackpot)
		);
		
		// 傳送給 client
		MessageAction.broadCastMessage(MessageTypes.S_BROCAST_JACKPOT_MONEY, response, BroadcastTargetTypes.BROADCAST_TO_ALL);
	}
}
